package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.common.CommonVo;
import com.lemon.smartsafety.entity.notice.NoticeInfoVo;
import com.lemon.smartsafety.entity.notice.NoticeReaderVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

/**
 * Created by Cooliean on 16/1/19.
 * 通知公告
 */
public class NoticeServices extends BaseHttpServices {
    private static      LoggerHelper loggerHelper = new LoggerHelper(NoticeServices.class);
    public static final String       TAG          = "NoticeServices";

    public NoticeServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static NoticeServices init() {
        return new NoticeServices(IApplication.context());
    }


    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public static NoticeServices init(Context context) {
        return new NoticeServices(context);
    }


    /**
     * 获取通知公告列表
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public NoticeServices getNoticeList(RefreshType type, int pageSize, int currentPage, String hasRead, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("keywords", StringUtils.nullValue(keyword));
        params.put("has_read", hasRead + "");
        return this.getNoticeList(type, params);
    }

    /**
     * 获取通知公告列表
     *
     * @param params
     * @return
     */
    private NoticeServices getNoticeList(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<NoticeInfoVo>> callback = new HandlerCallback<List<NoticeInfoVo>>(EventTag.TAG_NOTICE_LIST + params
                .get("has_read")) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.NOTICE_LIST);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 保存公告消息
     *
     * @return
     */
    public NoticeServices saveAction(LemonRequestParams params, boolean isEdit) {
        HandlerCallback<CommonVo> callback = new HandlerCallback<CommonVo>(EventTag.TAG_NOTICE_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(isEdit ? UrlInfo.NOTICE_SAVE_EDIT : UrlInfo.NOTICE_SAVE_NEW);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取已发通知公告列表
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public NoticeServices getSentNoticeList(RefreshType type, int pageSize, int currentPage, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("keywords", StringUtils.nullValue(keyword));
        params.put("has_read", "0");
        return this.getSentNoticeList(type, params);
    }

    /**
     * 获取已发通知公告列表
     *
     * @param params
     * @return
     */
    private NoticeServices getSentNoticeList(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<NoticeInfoVo>> callback = new HandlerCallback<List<NoticeInfoVo>>(EventTag.TAG_NOTICE_SENT_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.NOTICE_SENT_LIST);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }




    /**
     * 获取已发通知公告详情人员列表
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public NoticeServices getSentNoticeDetailList(RefreshType type, int pageSize, int currentPage,String noticeId,ReadType listType, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("keywords", StringUtils.nullValue(keyword));
        params.put("notice_id", noticeId);
        return this.getSentNoticeDetailList(type, params,listType);
    }

    public enum ReadType{
        READ(1), UN_READ(0);

        int value;
        ReadType(int type) {
            this.value = type;
        }

        public int getValue() {
            return value;
        }
    }

    /**
     * 获取已发通知公告详情人员列表
     *
     * @param params
     * @return
     */
    private NoticeServices getSentNoticeDetailList(RefreshType type, LemonRequestParams params,ReadType listType) {
        HandlerCallback<List<NoticeReaderVo>> callback = new HandlerCallback<List<NoticeReaderVo>>(EventTag.TAG_NOTICE_SENT_DETAIL_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.NOTICE_SENT_DETAIL_READ_LIST);
        if (ReadType.UN_READ == listType) {
            serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.NOTICE_SENT_DETAIL_NO_READ_LIST);
        }
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }
}
